package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQueryObject;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationRepository repository;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private ITravelService travelService;

    @Override
    public void save(Destination destination) {
        destination.setId(null);
        repository.save(destination);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public void update(Destination destination) {
        repository.save(destination);
    }

    @Override
    public Destination get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> listAll() {
        return repository.findAll();
    }

    @Override
    public List<Destination> queryDestByRegionId(String rid) {
        // 1.查询区域下目的地id集合
        Region region = regionService.get(rid);
        // 以目的地id集合作为添加查询目的地对象集合
        List<String> refIds = region.getRefIds();

        return repository.findByIdIn(refIds);
    }

    @Override
    public Page<Destination> query(DestinationQueryObject qo) {
        Query query = new Query();
        if(StringUtils.hasLength(qo.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }
        if(StringUtils.hasLength(qo.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }else {
            query.addCriteria(Criteria.where("parentId").is(null));
        }

        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.ASC,"_id");

        return DBHelper.query(template,pageable,query,Destination.class,qo);
    }

    @Override
    public List<Destination> queryToasts(String destId) {
        // 先创建一个集合
        ArrayList<Destination> list = new ArrayList<>();
        this.createToasts(list,destId);
        Collections.reverse(list);  // 取反   ABC  变成  CBA
        /*if(!StringUtils.hasLength(destId)){
            return list;
        }
        // 缺点：层级越多，需要写的循环多，不通用
        Destination dest = this.get(destId);//广州
        list.add(dest);
        if(StringUtils.hasLength(dest.getParentId())){
            Destination d = this.get(dest.getParentId());  // 广东
            list.add(d);
            if(StringUtils.hasLength(d.getParentId())){
                Destination dd = this.get(d.getParentId()); // 中国
                list.add(dd);
            }
        }*/
        // 优化：1.while循环  2.递归
        return list;
    }

    private void createToasts(List<Destination> list,String destId){
        if(!StringUtils.hasLength(destId)){
            return;
        }
        Destination dest = this.get(destId);//广州
        list.add(dest);
        if(StringUtils.hasLength(dest.getParentId())){
            this.createToasts(list,dest.getParentId());
        }
    }

    @Override
    public List<Destination> queryDestByRegionIdApi(String regionId) {

        // 根据区域查询的目的地是一个集合
        List<Destination> list = new ArrayList<>();
        // 国内
        if("-1".equals(regionId)){
            list = repository.findByParentName("中国");
        }else {
            // 国外
            list = this.queryDestByRegionId(regionId);
        }
        // 根据父级目的地查询该级别下的所有子目的地
        for (Destination destination : list) {
            Pageable pageable = PageRequest.of(0,5,Sort.Direction.ASC,"_id");
            List<Destination> children = repository.queryByParentId(destination.getId(),pageable);
            // 页面只显示五个数据
            /*if(children.size() > 5){
                children = children.subList(0,5);
            }*/
            destination.setChildren(children);
        }
        return list;
    }

    @Override
    public Destination getByName(String name) {
        return repository.queryByName(name);
    }


}
